<?php

class Model_barang extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function getAll() {
        $this->db->order_by('id_order', 'DESC');
        $query = $this->db->get('order');

        return $query->result();
    }
    
    function getAlls($perpage, $segment) {
       if ($segment == '') {
            $segment = 0;
        }
        $ljt = $segment;
        $query = $this->db->query("SELECT * FROM `order` ORDER BY id_order DESC LIMIT $ljt,$perpage");

        return $query->result();
    }

    function getBarang($id) {
        $this->db->where('id_order', $id);
        $query = $this->db->get('order');

        return $query->row_array();
    }

    function save() {
        $job_order = $this->input->post('job_order');
        $consgnee = $this->input->post('consgnee');
        $origin = $this->input->post('origin');
        $shipper = $this->input->post('shipper');
        $via = $this->input->post('via');
        $agent = $this->input->post('agent');
        $ctn = $this->input->post('ctn');
        $kg = $this->input->post('kg');
        $m3 = $this->input->post('m3');
        $description = $this->input->post('description');
        
        $penerimaan = $this->input->post('penerimaan');
        $cost = $this->input->post('cost');
        $profit = $penerimaan - $cost;
        $remarks = $this->input->post('remarks');
        
        $data = array(
            'job_order' => $job_order,
            'no_sj' => $job_order,
            'consgnee' => $consgnee,
            'origin' => $origin,
            'shipper' => $shipper,
            'via' => $via,
            'agent' => $agent,
            'ctn' => $ctn,
            'kg' => $kg,
            'm3' => $m3,
            'invoice' => $job_order,
            'description' => $description,
            'penerimaan' => $penerimaan,
            'cost' => $cost,
            'profit' => $profit,
            'remarks' => $remarks,
            'id_user1' => $this->session->userdata('id')
        );
        
        return $this->db->insert('order', $data);
    }

    function update() {
        $id_order = $this->input->post('id');
        $job_order = $this->input->post('job_order');
        $consgnee = $this->input->post('consgnee');
        $origin = $this->input->post('origin');
        $shipper = $this->input->post('shipper');
        $via = $this->input->post('via');
        $agent = $this->input->post('agent');
        $ctn = $this->input->post('ctn');
        $kg = $this->input->post('kg');
        $m3 = $this->input->post('m3');
        $description = $this->input->post('description');
        $penerimaan = $this->input->post('penerimaan');
        $cost = $this->input->post('cost');
        $profit = $penerimaan - $cost;
        $remarks = $this->input->post('remarks');
        
        $data = array(
            'job_order' => $job_order,
            'no_sj' => $job_order,
            'consgnee' => $consgnee,
            'origin' => $origin,
            'shipper' => $shipper,
            'via' => $via,
            'agent' => $agent,
            'ctn' => $ctn,
            'kg' => $kg,
            'm3' => $m3,
            'invoice' => $job_order,
            'description' => $description,
            'penerimaan' => $penerimaan,
            'cost' => $cost,
            'profit' => $profit,
            'remarks' => $remarks,
            'id_user1' => $this->session->userdata('id')
        );
        
        $this->db->where('id_order', $id_order);
        return $this->db->update('order', $data);
    }
    
    function updates() {
        $id_order = $this->input->post('id');
        $etd_sin = $this->input->post('etd_sin');
        $eta_jkt = $this->input->post('eta_jkt');
        $delivery = $this->input->post('delivery');
        
        $data = array(
            
            'etd_sin' => $etd_sin,
            'eta_jkt' => $eta_jkt,
            'delivery' => $delivery
        );
        
        $this->db->where('id_order', $id_order);
        return $this->db->update('order', $data);
    }
    
    function agree(){
        if($this->input->post('agree') == 'Y'){
            $data = array(
                'agree' => 'N'
            );
        }else{
            $data = array(
                'agree' => 'Y'
            );
        }
        
        $this->db->where('id_order', $this->input->post('id_order'));
        return $this->db->update('order', $data);
    }
    
    function agrees(){
        if($this->input->get('agree') == 'Y'){
            $data = array(
                'agree' => 'N'
            );
        }else{
            $data = array(
                'agree' => 'Y'
            );
        }
        
        $this->db->where('id_order', $this->input->get('id_order'));
        return $this->db->update('order', $data);
    }
    
    function updatess() {
        $id_order = $this->input->post('id');
        $jatuhtempo = $this->input->post('jatuhtempo');
        $keterangan = $this->input->post('keterangan');
        
        $data = array(
            'jatuh_tempo' => $jatuhtempo,
            'keterangan' => $keterangan
        );
        
        $this->db->where('id_order', $id_order);
        return $this->db->update('order', $data);
    }
    
    function cek_order($q){
        $q = $this->db->query("SELECT job_order FROM `order` WHERE job_order = '$q'");
        return $q->num_rows();
    }
    
    function search($q, $field){
        $query = $this->db->query("SELECT * FROM `order` WHERE $field LIKE '%$q%' ORDER BY id_order DESC");

        return $query->result();
    }

}

?>